package at.ltasat.classifier.knn;

import java.util.Arrays;

import at.ltasat.classifier.ObjectFeatures;

final class Point {
	private int[] coordinates;

	public static Point create(ObjectFeatures features, int factor) {
		int[] coordinates = new int[features.getNumberOfFeatures()];
		for (int i = 0; i < coordinates.length; i++) {
			coordinates[i] = (int) (features.getFeatureValue(i) * factor);
		}
		return new Point(coordinates);
	}

	public Point(int[] coordinates) {
		this.coordinates = coordinates;
	}

	public int getNumberOfDimensions() {
		return coordinates.length;
	}

	public int getCoordinate(int index) {
		return coordinates[index];
	}

	public double getDistanceTo(Point other) {
		int distance = 0;
		assert getNumberOfDimensions() == other.getNumberOfDimensions();
		for (int i = 0; i < getNumberOfDimensions(); i++) {
			int coordinateDelta = getCoordinate(i) - other.getCoordinate(i);
			distance += coordinateDelta * coordinateDelta;
		}
		return Math.sqrt(distance);
	}

	@Override
	public int hashCode() {
		return Arrays.hashCode(coordinates);
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Point) {
			Point other = (Point) obj;
			return Arrays.equals(coordinates, other.coordinates);
		}
		return false;
	}

	@Override
	public String toString() {
		return Arrays.toString(coordinates);
	}

}
